<style>
    .print-format {
        margin-left: 0mm;
        margin-right: 0mm;
        margin-top: 10mm;
        margin-bottom: 60mm;
        font-size: 10pt;
    }
</style>

{% set company = frappe.get_doc("Company", doc.company) %}
{% set company_address_name = frappe.db.get_value("Dynamic Link", filters={"link_doctype": "Company", "link_name": doc.company, "parenttype": "Address"}, fieldname="parent") %}
{% set cmp_addr = frappe.get_doc("Address", company_address_name) if company_address_name else None %}
{% set pay_addr = frappe.get_doc("Address", doc.customer_address) %}

{% if doc.contact_person %}
    {% set contact = frappe.get_doc("Contact", doc.contact_person) %}
{% endif %}

<!-- HEAD -->
<div id="header">
    <div class="letter-head">
        {{ letter_head }}
    </div>
</div>

<div class="contact-row">
    <div id="address">
        <div id="sender">
            <p>{{ cmp_addr.address_title }} &#183; {{ cmp_addr.address_line1 }} &#183;
            {% if cmp_addr.address_line2 %}{{ cmp_addr.address_line2 }}<br>{% endif %}
            {{ cmp_addr.pincode }} {{ cmp_addr.city }}
            {% if cmp_addr.country != pay_addr.country -%}
                    &#183; {{ cmp_addr.country | upper }}
            {%- endif %}
            </p>
        </div>
        {{ doc.customer_name }}<br />
        {% if contact %}
                {%- if contact.salutation -%}
                    {{ contact.salutation }}
                {%- endif %}
                {%- if contact.salutation in ["Hr.", "Herr"] %}n{% endif %}
                {{ contact.first_name }} {{ contact.last_name }}<br>
        {% endif %}
        {{ doc.address_display }}
    </div>

    <div id="contact">
        <table>
            {% if company.phone_no %}
            <tr>
                <td>{{ _("Phone") }}:</td>
                <td class="text-right">{{ company.phone_no }}</td>
            </tr>
            {% endif %}

            {% if company.email %}
            <tr>
                <td>{{ _("Email") }}:</td>
                <td class="text-right">{{ company.email }}</td>
            </tr>
            {% endif %}
            <tr>
                <td>{{ _("Date") }}:</td>
                <td class="text-right">{{ frappe.utils.formatdate(doc.transaction_date, "long") }}</td>
            </tr>
            <!--<tr>
                <td>{{ _("Delivery Date") }}:</td>
                <td class="text-right">{{ frappe.utils.formatdate(doc.delivery_date, "long") }}</td>
            </tr>-->
            <tr>
                <td>{{ _("Our Sign") }}:</td>
                <td class="text-right">{{ doc.name }}</td>
            </tr>
            {% if doc.po_no %}
            <tr>
                <td>{{ _("Your Sign") }}:</td>
                <td class="text-right">{{ doc.po_no }}</td>
            </tr>
            {% endif %}
        </table>
    </div>
</div>

<div id="faltmarke-1" class="din-mark"></div>
<div id="lochmarke" class="din-mark"></div>
<div id="faltmarke-2" class="din-mark"></div>

<!-- CONTENT -->
<div id="text" class="">
	<div id="subject">
		<strong>{{ _(doc.doctype) }}: {{ doc.name }}</strong>
    </div>
    <div class="salutation">
        {% if frappe.lang == "de" %}
            {% if not contact %}
                <p>Sehr geehrte Damen und Herren,</p>
            {% else %}
                <p>Sehr geehrte{% if contact.salutation in ["Hr.", "Herr"] %}r{% endif %} {{ contact.salutation }} {{ contact.last_name }},<p>
            {% endif %}
            <br>
            <p>hiermit bestätigen wir Ihren Auftrag über die folgenden Positionen:</p>
        {% else %}
            {% if not contact %}
                <p>Dear Sir or Madam,</p>
            {% else %}
                <p>Dear {{ contact.salutation }} {{ contact.last_name }},<p>
            {% endif %}
            <br>
            <p>we are pleased to confirm your order of the following items:</p>
        {% endif %}
    </div>

    <br>

    <table class="w-100 text-small">
        <thead class="black-border">
            <tr>
                <th style="width: 5%">{{ _("Sr") }}</th>
                <th style="width: 45%">{{ _("Description") }}</th>
                <th style="width: 10%">{{ _("Unit") if not print_settings.print_uom_after_quantity else '&nbsp;'}}</th>
                <th style="width: 10%">{{ _("Qty") }}</th>
                <th style="width: 15%">{{ _("Price") }}</th>
                <!-- <th style="width: 15%">{{ _("Discount") }}</th> -->
                <th style="width: 15%" class="text-right">{{ _("Amount") }}</th>
            </tr>
        </thead>

        <tbody>
            {% for n in doc.items %}
            <tr>
                <td>{{ loop.index }}</td>
                <td><b>{{ _(n.item_name) }}</b><br>
                    {{ n.description }}
                    {% if n.prevdoc_docname %}
                    <!-- Angebot: A-2020-001 -->
                    ({{ _("Quotation") }}: {{ n.prevdoc_docname }})
                    {% endif %}
                </td>
                <td>{{ _(n.uom) if not print_settings.print_uom_after_quantity else '&nbsp;'}}</td>
                <td>
                    {{ n.get_formatted("qty") }}
                    {% if print_settings.print_uom_after_quantity %}
                        {{ _(n.uom) }}
                    {% endif %}
                </td>
                <td>{{ n.get_formatted("rate") }}</td>
                <!-- <td>{{ n.get_formatted("discount_amount") }}</td> -->
                <td class="text-right">{{ n.get_formatted("amount") }}</td>
            </tr>
            {% endfor %}
        </tbody>

        <tfoot class="black-border">
            {% if doc.additional_discount_percentage %}
                <tr>
                    <td></td>
                    <td>{{ _("Total") }}</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td class="text-right">{{ doc.get_formatted("total") }}</td>
                </tr>
                <tr>
                    <td></td>
                    <td>{{ _("Discount") }}</td>
                    <td></td>
                    <td></td>
                    <td>
                        {{ doc.get_formatted("additional_discount_percentage") }} &percnt;
                    </td>
                    <td class="text-right">
                        {{ doc.get_formatted("discount_amount") }}
                    </td>
                </tr>
            {% endif %}
            <tr>
                <td></td>
                <td>{{ _("Net Total") }}</td>
                <td></td>
                <td></td>
                <td></td>
                <td class="text-right">{{ doc.get_formatted("net_total") }}</td>
            </tr>
            {% if doc.tax_category == "Reverse Charge" -%}
                <tr>
                    <td></td>
                    <td>Steuerschuldnerschaft des Leistungsempfängers.</td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td class="text-right"></td>
                </tr>
            {% else %}
                {% for tax_row in doc.taxes -%}
                    {% if tax_row.tax_amount or print_settings.print_taxes_with_zero_amount %}
                        <tr>
                            <td></td>
                            <td>
                            {{ tax_row.description }}
                            </td>
                            <td></td>
                            <td></td>
                            <td></td>
                            <td class="text-right">
                                {{ tax_row.get_formatted("tax_amount") }}
                            </td>
                        </tr>
                  {% endif %}
                {%- endfor %}
            {%- endif %}
            <tr>
                <td></td>
                <td>
                    <strong>{{ _("Grand Total") }}</strong>
                </td>
                <td></td>
                <td></td>
                <td></td>
                <td class="text-right">
                    <strong>{{ doc.get_formatted("grand_total") }}</strong>
                </td>
            </tr>
            {% if doc.advance_paid %}
                <tr>
                    <td></td>
                    <td>
                    {{ _("Total Advance") }}
                    </td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td class="text-right">
                        {{ doc.get_formatted("advance_paid") }}
                    </td>
                </tr>
            {% endif %}
        </tfoot>
    </table>

    <br>

    <div class="epilogue">
        {% if (doc.terms | striptags) %}
            <div class="terms">
                {{ doc.terms }}
            </div>
            <br>
        {% endif %}
        {% if frappe.lang == "de" -%}
            <p>Vielen Dank für Ihren Auftrag. Für Anmerkungen oder Fragen stehen wir gerne zur Verfügung.</p>
            <br>
            <div class="salutation">
                <p>Beste Grüße nach {{ pay_addr.city }}</p>
                <p>Ihr Team der {{ company.company_name }}</p>
            </div>
        {% else %}
            <p>Thank you for your order. If you have any comments or questions, please do not hesitate to contact us.</p>
            <br>
            <div class="salutation">
                <p>Best regards,</p>
                <p>Your team at {{ company.company_name }}</p>
            </div>
        {%- endif %}
    </div>
</div>

<!-- FOOTER -->
<div id="footer-html" class="visible-pdf">
    <p id="pagenum">
        {{ _("Page {0} of {1}").format('<span class="page"></span>', '<span class="topage"></span>') }}
    </p>
    <div class="print-format-footer">
        {{ footer }}
    </div>
</div>
